package com.mmlc.utils;

import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.widget.Toast;

import java.util.Locale;

/**
 * Created by admin on 2018/1/12.
 */

public class TextToSpeek implements TextToSpeech.OnInitListener {

    private final TextToSpeech mTextToSpeech;//TTS对象
    private Context mContext;

    String msg ="";
    public TextToSpeek(Context context,String msg){
        this.mContext=context;//获取上下文
        this.mTextToSpeech=new TextToSpeech(this.mContext,this);//实例化TTS
        this.msg=msg;
    }

    //初始化TTS引擎
    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            int result = mTextToSpeech.setLanguage(Locale.CHINA);
            int result2 = mTextToSpeech.setLanguage(Locale.ENGLISH);
            if (result == TextToSpeech.LANG_MISSING_DATA ||result2 == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Toast.makeText(mContext, "数据丢失或不支持", Toast.LENGTH_SHORT).show();
            }
        }
        if(!msg.equals("")){
            mTextToSpeech.speak(msg,TextToSpeech.QUEUE_FLUSH, null);
        }
    }

    public void stop(){
        mTextToSpeech.stop();
    }


    public void relese(){
        mTextToSpeech.stop();
        mTextToSpeech.shutdown();
    }

}
